class LocationManager():
    """
    A class which is responsible for initializes in-memory arrays of location updates per tick.
    """
    def __init__(self, locations_filename):
        """
        Constructor.
        """
        self._filename = locations_filename
        self._ticks = None
        self._number_of_nodes = None
        self._max_x = None
        self._max_y = None
        self._location_updates = {}
        
        self._initialize()
        
    def _initialize(self):
        """
        Reads the input file and initializes the in-memory array in order to boost performance.
        """
        f = open(self._filename)
        lines = f.readlines()
        self._initialize_metadata(lines[0])
        self._initialize_location_updates(lines[1:])
        f.close()
        
    def _initialize_metadata(self, data_lines):
        """
        Reads the metadata line from the input file.
        """
        data = data_lines.split(",")
        self._number_of_nodes = int(data[0][data[0].index("=")+1:])
        self._ticks = int(data[1][data[1].index("=")+1:])
        self._max_x = int(data[2][data[2].index("=")+1:])
        self._max_y = int(data[3][data[3].index("=")+1:])
        
    def _initialize_location_updates(self, update_lines):
        """
        Reads the location updates from the input file.
        """
        for line in update_lines:
            if line[0] == "#" or len(line.strip()) == 0:
                continue
                
            data = line.strip().split(",")
            current_tick = int(data[0][1:]) #Remove the "T" heading from every line in the traffic file.
            current_updates = {}
            current_node_id = 0
            
            for i in xrange(1, len(data), 2):
                current_updates[current_node_id] = (int(data[i]), int(data[i+1]))
                current_node_id += 1
                
            self._location_updates[current_tick] = current_updates
            
    def get_number_of_ticks(self):
        """
        Returns the number of ticks (iterations) in the simulation.
        """
        return self._ticks
        
    def get_number_of_nodes(self):
        """
        Returns the number of users in the simulation.
        """
        return self._number_of_nodes

    def get_max_x(self):
        """
        Returns the maximum x coordinate for the locations.
        """
        return self._max_x

    def get_max_y(self):
        """
        Returns the maximum y coordinate for the locations.
        """
        return self._max_y

    def get_location_updates_by_tick(self, tick):
        """
        Returns all the location of all the nodes for some tick.
        """
        return self._location_updates[tick]
        
    
